home *** CD-ROM | disk | FTP | other *** search
- Path: library.erc.clarkson.edu!rpi!not-for-mail
- From: Shalom Reich <sqr1874@acf4.nyu.edu>
- Newsgroups: comp.lang.c++,comp.lang.c++.moderated
- Subject: Re: Argc & Argv
- Date: 28 Feb 1996 15:00:58 -0000
- Organization: Bankers Trust Company
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: Dietmar.Kuehl@uni-konstanz.de
- Message-ID: <4h1qna$8is@netlab.cs.rpi.edu>
- References: <4gta9f$df5@netlab.cs.rpi.edu>
- NNTP-Posting-Host: netlab.cs.rpi.edu
- X-Original-Date: Tue, 27 Feb 1996 13:35:07 -0500
-
- Rick Richert wrote:
- >
- > I have some global objects defined outside the main routine and I would
- > like to pass the argc and argv values to their constructors. For example,
- >
- >
- > SomeClass obj( argc, argv);
- >
- > int main( int argc, char **argv) {
- >
- > blah, blah, blah
- >
- > }
- >
- > Unfortunately, the compiler tells me that argc and argv are not defined
- > or else not available for obj.
- >
- > Currently, I get around this problem by creating the global obj and then
- > inside of main, I initialize obj via a method that takes argc and argv.
- >
- > I would prefer to pass the arg vars to the constructor. Does anyone know
- > how I can pass the arg vars without being inside of main?
- >
-
- The obj object is defined outside of main which means the constructor will
- be called before main is started. In this case argc and argv don't exist
- when the obj constructor is called and your compiler is correct.
-
- Since you don't have the information needed to initialize the object, the
- question becomes whether you really want to create the object at this point
- or whether you need a global handle to the object to be available (whenever
- it gets created).
-
- If a global handle is enough, you might want to change your code as follows:
-
-
- SomeClass * obj;
-
- int main( int argc, char **argv) {
-
- obj = new SomeClass( argc, argv);
-
- blah, blah, blah
-
- }
-
- The obj object will now be created where you were initializing the
- members.
-
- Since obj is now a *pointer* instead of an object all references to
- obj's members/functions need to be changed from obj.xxx to obj->xxx.
-
- I hope this helps.
-
- Shalom Reich
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-